package com.bw.shopping.mvvm.viewmodel;

import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;

import com.blankj.utilcode.util.LogUtils;
import com.bw.mvvm.entity.BaseEntity;
import com.bw.mvvm.entity.SeckillEntity;
import com.bw.mvvm.fleld.UIField;
import com.bw.mvvm.mvvm.viewmodel.BaseViewModel;
import com.bw.shopping.adapter.CartAdapter;
import com.bw.shopping.adapter.SeckillAdapter;
import com.bw.shopping.mvvm.model.SeckillModel;
import com.bw.shopping.mvvm.model.entity.SeckillAction;
import com.bw.shopping.mvvm.view.SeckillParticulaesActivity;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;

import org.greenrobot.eventbus.EventBus;
import org.jetbrains.annotations.NotNull;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import okhttp3.RequestBody;

public class SeckillViewModel extends BaseViewModel<SeckillModel> {

    public BaseQuickAdapter adapter = new SeckillAdapter();
    public List<SeckillEntity.DataBean> dataBeans = new ArrayList<>();
    public ObservableField<String> endTime = new ObservableField<>();
    public ObservableField<String> startTime = new ObservableField<>();
    @Override
    protected void result(BaseEntity entity) {
        if (entity instanceof SeckillEntity){
            SeckillEntity seckillEntity = (SeckillEntity) entity;
            adapter.setNewInstance(seckillEntity.getData());
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            long st = seckillEntity.getData().get(0).getStarttime();
            long startTimeLong = st*1000;
            String start = formatter.format(startTimeLong);
            startTime.set(start);
            SimpleDateFormat endSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            long end = seckillEntity.getData().get(0).getEndtime();
            long endTimeLong = end * 1000;
            String ed = endSdf.format(endTimeLong);
            endTime.set(ed);
            dataBeans.addAll(seckillEntity.getData());

        }
        adapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull @NotNull BaseQuickAdapter adapter, @NonNull @NotNull View view, int position) {
                EventBus.getDefault().postSticky(dataBeans.get(position));
                SeckillAction action = new SeckillAction();
                action.action = "ScekillValue";
                EventBus.getDefault().postSticky(action);
                HashMap<String,Object> map = new HashMap<>();
                map.put(UIField.PAGECS, SeckillParticulaesActivity.class);
                startPage(map);
            }
        });

    }

    @Override
    public void onCreate() {
        super.onCreate();
        m.entity().subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(this);
    }

    public void onBack(){
        finishPage();
    }

}
